/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { RouteRecordName, RouteRecordRaw } from 'vue-router'
import type { VabRouteRecord } from '/#/router'
import {
  createRouter,
  createWebHashHistory,
  createWebHistory,
} from 'vue-router'
import Layout from '@vab/layouts/index.vue'
import { setupPermissions } from './permissions'
import { authentication, isHashRouterMode, publicPath } from '@/config'

export const constantRoutes: VabRouteRecord[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/callback',
    name: 'Callback',
    component: () => import('@/views/callback/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404.vue'),
    meta: {
      hidden: true,
    },
  },
  // {
  //   path: '/trackingList',
  //   name: 'TrackingList',
  //   component: () => import('@/views/customer/tracking/tracklist/index.vue'),
  //   meta: {
  //     title: '跟踪记录',
  //   },
  // },
]

export const asyncRoutes: VabRouteRecord[] = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('@/views/index/index.vue'),
        meta: {
          title: '首页工作台',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/index/dashboard.vue'),
        meta: {
          title: '数据统计看板',
          icon: 'bar-chart-box-line',
        },
      },
      // {
      //   path: 'remixIcon',
      //   name: 'RemixIcon',
      //   component: () => import('@/views/vab/icon/remixIcon.vue'),
      //   meta: {
      //     title: '图标采纳',
      //     icon: 'image-2-line',
      //   },
      // },
    ],
  },
  // {
  //   path: '/systemset',
  //   name: 'SystemSet',
  //   component: Layout,
  //   meta: {
  //     title: '设置',
  //     icon: 'home-2-line',
  //     breadcrumbHidden: true,
  //   },
  // },
  {
    path: '/area',
    name: 'Area',
    component: Layout,
    meta: {
      title: '区域',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'areaIndex',
        name: 'AreaIndex',
        component: () => import('@/views/area/index.vue'),
        meta: {
          title: '区域管理',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
    ],
  },
  {
    path: '/community',
    name: 'Community',
    component: Layout,
    meta: {
      title: '园区',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'communityIndex',
        name: 'CommunityIndex',
        component: () => import('@/views/community/index.vue'),
        meta: {
          title: '园区管理',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'building',
        name: 'Building',
        component: () => import('@/views/community/building/index.vue'),
        meta: {
          title: '楼幢管理',
          icon: 'bar-chart-box-line',
          hidden: true,
        },
        children: [
          {
            path: 'buildingIndex',
            name: 'BuildingIndex',
            component: () => import('@/views/community/building/index.vue'),
            meta: {
              title: '楼幢管理',
              icon: 'bar-chart-box-line',
              hidden: true,
            },
          },
          {
            path: 'buildingShow',
            name: 'BuildingShow',
            component: () => import('@/views/community/building/show.vue'),
            meta: {
              title: '楼幢展示',
              icon: 'bar-chart-box-line',
              hidden: true,
            },
          },
        ],
      },
    ],
  },
  {
    path: '/industry',
    name: 'Industry',
    component: Layout,
    meta: {
      title: '行业',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'industryIndex',
        name: 'IndustryIndex',
        component: () => import('@/views/industry/index.vue'),
        meta: {
          title: '行业管理',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/users',
    name: 'Users',
    component: Layout,
    meta: {
      title: '用户',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'userIndex',
        name: 'UserIndex',
        component: () => import('@/views/users/index.vue'),
        meta: {
          title: '用户统计',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'person',
        name: 'Person',
        component: () => import('@/views/users/person/index.vue'),
        meta: {
          title: '个人用户',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'company',
        name: 'Company',
        component: () => import('@/views/users/company/index.vue'),
        meta: {
          title: '单位用户',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/apps',
    name: 'Apps',
    component: Layout,
    meta: {
      title: '应用',
      icon: 'home-2-line',
      breadcrumbHidden: true,
    },
    children: [
      {
        path: 'appIndex',
        name: 'AppIndex',
        component: () => import('@/views/apps/index.vue'),
        meta: {
          title: '应用首页',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'appClass',
        name: 'AppClass',
        component: () => import('@/views/apps/class/index.vue'),
        meta: {
          title: '应用分类',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'applist',
        name: 'AppList',
        component: () => import('@/views/apps/app/index.vue'),
        meta: {
          title: '应用',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'webset',
        name: 'WebSite',
        // component: () => import('@/views/apps/app/website/index.vue'),
        meta: {
          title: '网站系统',
          icon: 'bar-chart-box-line',
        },
        children: [
          {
            path: 'websiteIndex',
            name: 'WebSiteIndex',
            component: () => import('@/views/apps/website/index.vue'),
            meta: {
              title: '统计',
              icon: 'home-2-line',
              noClosable: true,
            },
          },
          {
            path: 'websiteSetup',
            name: 'WebSiteSetup',
            component: () => import('@/views/apps/website/setup/index.vue'),
            meta: {
              title: '设置',
              icon: 'home-2-line',
              noClosable: true,
            },
          },
          {
            path: 'websiteTemplate',
            name: 'WebSiteTemplate',
            component: () => import('@/views/apps/website/template/index.vue'),
            meta: {
              title: '模板',
              icon: 'home-2-line',
              noClosable: true,
            },
          },
          {
            path: 'websiteUsers',
            name: 'WebSiteUsers',
            component: () => import('@/views/apps/website/users/index.vue'),
            meta: {
              title: '用户',
              icon: 'home-2-line',
              noClosable: true,
            },
          },
        ],
      },
      {
        path: 'hr',
        name: 'HR',
        component: () => import('@/views/apps/hr/index.vue'),
        meta: {
          title: '组织管理系统',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'okr',
        name: 'OKR',
        component: () => import('@/views/apps/okr/index.vue'),
        meta: {
          title: '目标管理系统',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'crm',
        name: 'CRM',
        component: () => import('@/views/apps/crm/index.vue'),
        meta: {
          title: '客户服务系统',
          icon: 'bar-chart-box-line',
        },
      },
      {
        path: 'marketing',
        name: 'Marketing',
        component: () => import('@/views/apps/marketing/index.vue'),
        meta: {
          title: '营销系统',
          icon: 'bar-chart-box-line',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode
    ? createWebHashHistory(publicPath)
    : createWebHistory(publicPath),
  routes: constantRoutes as RouteRecordRaw[],
})

function fatteningRoutes(routes: VabRouteRecord[]): VabRouteRecord[] {
  return routes.flatMap((route: VabRouteRecord) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

function addRouter(routes: VabRouteRecord[]) {
  routes.forEach((route: VabRouteRecord) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export function resetRouter(routes: VabRouteRecord[] = constantRoutes) {
  routes.map((route: VabRouteRecord) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach(({ name }) => {
    router.hasRoute(<RouteRecordName>name) &&
      router.removeRoute(<RouteRecordName>name)
  })
  addRouter(routes)
}

export function setupRouter(app: any) {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
